package com.gzsxy.NIO;

import java.io.FileOutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * @author xiaolong
 * @version 1.0
 * @description: 通过fileChannel通道进行文本写入
 * @date 2022/4/23 19:58
 */
public class NIOFileChannel01 {

    public static void main(String[] args) throws Exception {
        String str = "hello,尚硅谷";
        //创建一个输出流 -》channel
        FileOutputStream fileOutputStream = new FileOutputStream("d:\\file01.txt");
        //通过fileOutputStream 获取 对应的FileChannel
        //这个 fileChannel 真实类型是FileChannelImpl
        FileChannel fileChannel = fileOutputStream.getChannel();
        //创建一个缓冲区ByteBuffer
        ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
        //将str 放入byteBuffer
        byteBuffer.put(str.getBytes());


        //对byteBuffer 数据写入fileChannel
        byteBuffer.flip();

        fileChannel.write(byteBuffer);
        fileOutputStream.close();

    }
}
